package com.example.administrator.button;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import static com.example.administrator.button.Calculate.arrange;
import static com.example.administrator.button.StringExam.*;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView edit_screen;
    TextView result_screen;

    Button buttonToFunc;

    Button buttonCle;
    Button buttonPlu;
    Button buttonPow;
    Button buttonMin;
    Button buttonMul;
    Button buttonDev;
    Button buttonEqu;
    Button buttonDel;
    Button button0;
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    Button button6;
    Button button7;
    Button button8;
    Button button9;

    ImageButton btn_det;
    StringBuffer text = new StringBuffer();
    String s = new String();
    boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initButton();
    }

    //初始化控件资源
    public void initButton() {


        edit_screen = (TextView) this.findViewById(R.id.text_screen);
        result_screen = (TextView) this.findViewById(R.id.text_result);
        buttonCle = (Button) findViewById(R.id.btn_clear);
        buttonDev = (Button) findViewById(R.id.btn_devide);
        buttonEqu = (Button) findViewById(R.id.btn_calculate);
        buttonMin = (Button) findViewById(R.id.btn_minus);
        buttonMul = (Button) findViewById(R.id.btn_multiply);
        buttonPlu = (Button) findViewById(R.id.btn_plus);
        buttonPow = (Button) findViewById(R.id.btn_power);
        buttonDel = (Button) findViewById(R.id.btn_del);

        button0 = (Button) findViewById(R.id.btn_0);
        button1 = (Button) findViewById(R.id.btn_1);
        button2 = (Button) findViewById(R.id.btn_2);
        button3 = (Button) findViewById(R.id.btn_3);
        button4 = (Button) findViewById(R.id.btn_4);
        button5 = (Button) findViewById(R.id.btn_5);
        button6 = (Button) findViewById(R.id.btn_6);
        button7 = (Button) findViewById(R.id.btn_7);
        button8 = (Button) findViewById(R.id.btn_8);
        button9 = (Button) findViewById(R.id.btn_9);
        btn_det = (ImageButton) findViewById(R.id.btn_det);

        button0.setOnClickListener(this);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        buttonDev.setOnClickListener(this);
        buttonEqu.setOnClickListener(this);
        buttonMul.setOnClickListener(this);
        buttonMin.setOnClickListener(this);
        buttonPlu.setOnClickListener(this);
        buttonPow.setOnClickListener(this);
        buttonDel.setOnClickListener(this);
        findViewById(R.id.btn_leftBracket).setOnClickListener(this);
        findViewById(R.id.btn_rightBracket).setOnClickListener(this);
        buttonCle.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                text = new StringBuffer();
                s = "";
                edit_screen.setText("0");
                result_screen.setText("");
                return false;
            }
        });

        btn_det.setOnClickListener(this);

        buttonToFunc = findViewById(R.id.button_to_func);
        buttonToFunc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, FuncActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        int textLen = text.length();
        switch (v.getId()) {
            case R.id.btn_0:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('0');
                }
                break;
            case R.id.btn_1:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('1');
                }
                break;
            case R.id.btn_2:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('2');
                }
                break;
            case R.id.btn_3:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('3');
                }
                break;
            case R.id.btn_4:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('4');
                }
                break;
            case R.id.btn_5:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('5');
                }
                break;
            case R.id.btn_6:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('6');
                }
                break;
            case R.id.btn_7:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('7');
                }
                break;
            case R.id.btn_8:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('8');
                }
                break;
            case R.id.btn_9:
                if (flag) {
                    text = new StringBuffer();
                    textLen = 0;
                }
                if (textLen == 0 || text.charAt(textLen - 1) != ')') {
                    text.append('9');
                }
                break;
            case R.id.btn_del:
                if (textLen == 0) {
                    text.append("0.");
                } else if (isNum(text.charAt(textLen - 1)) && judgeDel(text.toString()) == false) {
                    text.append('.');
                }
                break;
            case R.id.btn_leftBracket:
                if (textLen == 0 || (textLen > 0 && text.charAt(textLen - 1) != '.' && isNum(text.charAt(textLen - 1)) == false))
                    text.append('(');
                break;
            case R.id.btn_rightBracket:
                if (cntBrackets(text.toString()) > 0 && textLen > 0 && !isOperation(text.charAt(textLen - 1)) && text.charAt(textLen - 1) != '.' && text.charAt(textLen - 1) != '(')
                    text.append(')');
                break;
            case R.id.btn_plus:
                if (textLen > 0 && (isNum(text.charAt(textLen - 1)) || text.charAt(textLen - 1) == ')')) {
                    text.append('+');
                } else if (textLen > 0 && cntCharacter(text.toString()) == 1)
                    text.setCharAt(textLen - 1, '+');
                break;
            case R.id.btn_minus:
                if (textLen == 0) {
                    text.append('-');
                } else if (textLen == 1) {
                    if (text.charAt(textLen - 1) != '-')
                        text.append('-');
                } else if (textLen == 2 && text.charAt(0) == '(') {
                    if (text.charAt(1) != '-')
                        text.append('-');
                } else if (text.charAt(textLen - 1) != '.' && cntCharacter(text.toString()) < 2 || (cntCharacter(text.toString()) == 2 && (text.charAt(textLen - 1) != '-' && text.charAt(textLen - 2) != '-')) || text.charAt(textLen - 1) == '(')
                    text.append('-');
                break;
            case R.id.btn_multiply:
                if (textLen > 0 && (isNum(text.charAt(textLen - 1)) || text.charAt(textLen - 1) == ')')) {
                    text.append('×');
                } else if (textLen > 0 && cntCharacter(text.toString()) == 1)
                    text.setCharAt(textLen - 1, '×');
                break;
            case R.id.btn_devide:
                if (textLen > 0 && (isNum(text.charAt(textLen - 1)) || text.charAt(textLen - 1) == ')')) {
                    text.append('÷');
                } else if (textLen > 0 && cntCharacter(text.toString()) == 1)
                    text.setCharAt(textLen - 1, '÷');
                break;
            case R.id.btn_power:
                if (textLen > 0 && (isNum(text.charAt(textLen - 1)) || text.charAt(textLen - 1) == ')')) {
                    text.append('^');
                } else if (cntCharacter(text.toString()) == 1)
                    text.setCharAt(textLen - 1, '^');
                break;
            case R.id.btn_clear:
                if (textLen > 0)
                    text.deleteCharAt(textLen - 1);
                if (text.length() == 0)
                    s = "";
                break;
            case R.id.btn_det:
                if (textLen > 0) {
                    text.deleteCharAt(textLen - 1);
                    break;
                } else if (text.length() == 0)
                    s = "";
                break;
            case R.id.btn_calculate:
                if (!edit_screen.getText().equals(result_screen.getText())) {
                    flag = true;
                }
                try {
                    text = new StringBuffer(s);
                    edit_screen.setText(devideByDel(s));
                    result_screen.setText("");
                } catch (Exception e) {
                    edit_screen.setText("bug");
                    text = new StringBuffer();
                    s = "";
                }
                break;
        }
        if (v.getId() != R.id.btn_calculate) {
            flag = false;
            edit_screen.setText(parseStringAndDevideByDel(text.toString()));
            if (text.length() > 0)
                s = completeString(text.toString());
            if (s.length() > 0) {
                try {
                    s = arrange(s);
                    Float d = Float.parseFloat(s);
                    s = d.toString();
                } catch (infinityException e) {
                    s = "∞";
                } catch (Exception e) {

                }
            }

            result_screen.setText(devideByDel(s));
        }
    }
}
